from django.db import models

# Create your models here.
from utils.models import BaseModel
from user.models import User
from django.db import models


class SysConfig(BaseModel):
    config_id = models.AutoField(primary_key=True, verbose_name='参数主键')
    config_name = models.CharField(max_length=100, default='', blank=True, verbose_name='参数名称')
    config_key = models.CharField(max_length=100, default='', blank=True, verbose_name='参数键名')
    config_value = models.CharField(max_length=500, default='', blank=True, verbose_name='参数键值')
    config_type = models.CharField(max_length=1, default='N', blank=True, verbose_name='系统内置（Y是 N否）')

    class Meta:
        db_table = 'yv_sys_config'
        verbose_name = '参数配置表'
        verbose_name_plural = verbose_name


class SysDept(BaseModel):
    dept_id = models.BigAutoField(primary_key=True, verbose_name='部门ID')
    parent_id = models.BigIntegerField(default=0, blank=True, verbose_name='父部门ID')
    ancestors = models.CharField(max_length=50, default='', blank=True, verbose_name='祖级列表')
    dept_name = models.CharField(max_length=30, default='', blank=True, verbose_name='部门名称')
    order_num = models.IntegerField(default=0, blank=True, verbose_name='显示顺序')
    leader = models.CharField(max_length=20, null=True, blank=True, verbose_name='负责人')
    phone = models.CharField(max_length=11, null=True, blank=True, verbose_name='联系电话')
    email = models.CharField(max_length=50, null=True, blank=True, verbose_name='邮箱')
    status = models.CharField(max_length=1, default='0', blank=True, verbose_name='部门状态（0正常 1停用）')
    del_flag = models.CharField(max_length=1, default='0', blank=True, verbose_name='删除标志（0代表存在 2代表删除）')

    class Meta:
        db_table = 'yv_sys_dept'
        verbose_name = '部门表'
        verbose_name_plural = verbose_name


class SysDictData(BaseModel):
    dict_code = models.BigAutoField(primary_key=True, verbose_name='字典编码')
    dict_sort = models.IntegerField(default=0, blank=True, verbose_name='字典排序')
    dict_label = models.CharField(max_length=100, default='', blank=True, verbose_name='字典标签')
    dict_value = models.CharField(max_length=100, default='', blank=True, verbose_name='字典键值')
    dict_type = models.CharField(max_length=100, default='', blank=True, verbose_name='字典类型')
    css_class = models.CharField(max_length=100, null=True, blank=True, verbose_name='样式属性（其他样式扩展）')
    list_class = models.CharField(max_length=100, null=True, blank=True, verbose_name='表格回显样式')
    is_default = models.CharField(max_length=1, default='N', blank=True, verbose_name='是否默认（Y是 N否）')
    status = models.CharField(max_length=1, default='0', blank=True, verbose_name='状态（0正常 1停用）')

    class Meta:
        db_table = 'yv_sys_dict_data'
        verbose_name = '字典数据表'
        verbose_name_plural = verbose_name


class SysDictType(BaseModel):
    dict_id = models.BigAutoField(primary_key=True, verbose_name='字典主键')
    dict_name = models.CharField(max_length=100, default='', blank=True, verbose_name='字典名称')
    dict_type = models.CharField(max_length=100, unique=True, blank=True, verbose_name='字典类型')
    status = models.CharField(max_length=1, default='0', blank=True, verbose_name='状态（0正常 1停用）')

    class Meta:
        db_table = 'yv_sys_dict_type'
        verbose_name = '字典类型表'
        verbose_name_plural = verbose_name


class SysJob(BaseModel):
    job_id = models.BigAutoField(primary_key=True, verbose_name='任务ID')
    job_name = models.CharField(max_length=64, verbose_name='任务名称')
    job_group = models.CharField(max_length=64, default='DEFAULT', verbose_name='任务组名')
    invoke_target = models.CharField(max_length=500, verbose_name='调用目标字符串')
    cron_expression = models.CharField(max_length=255, default='', blank=True, verbose_name='cron执行表达式')
    misfire_policy = models.CharField(max_length=20, default='3', blank=True, verbose_name='计划执行错误策略')
    concurrent = models.CharField(max_length=1, default='1', blank=True, verbose_name='是否并发执行')
    status = models.CharField(max_length=1, default='0', blank=True, verbose_name='状态（0正常 1暂停）')

    class Meta:
        db_table = 'yv_sys_job'
        verbose_name = '定时任务调度表'
        verbose_name_plural = verbose_name


class SysJobLog(BaseModel):
    job_log_id = models.BigAutoField(primary_key=True, verbose_name='任务日志ID')
    job_name = models.CharField(max_length=64, verbose_name='任务名称')
    job_group = models.CharField(max_length=64, verbose_name='任务组名')
    invoke_target = models.CharField(max_length=500, verbose_name='调用目标字符串')
    job_message = models.CharField(max_length=500, blank=True, null=True, verbose_name='日志信息')
    status = models.CharField(max_length=1, default='0', verbose_name='执行状态（0正常 1失败）')
    exception_info = models.CharField(max_length=2000, default='', blank=True, verbose_name='异常信息')

    class Meta:
        db_table = 'yv_sys_job_log'
        verbose_name = '定时任务调度日志表'


class SysLogininfor(BaseModel):
    info_id = models.BigAutoField(primary_key=True, verbose_name='访问ID')
    user_name = models.CharField(max_length=50, default='', blank=True, verbose_name='用户账号')
    ipaddr = models.CharField(max_length=128, default='', blank=True, verbose_name='登录IP地址')
    login_location = models.CharField(max_length=255, default='', blank=True, verbose_name='登录地点')
    browser = models.CharField(max_length=50, default='', blank=True, verbose_name='浏览器类型')
    os = models.CharField(max_length=50, default='', blank=True, verbose_name='操作系统')
    status = models.CharField(max_length=1, default='0', verbose_name='登录状态（0成功 1失败）')
    msg = models.CharField(max_length=255, default='', blank=True, verbose_name='提示消息')
    login_time = models.DateTimeField(blank=True, null=True, verbose_name='访问时间')

    class Meta:
        db_table = 'yv_sys_logininfor'
        verbose_name = '系统访问记录'


class SysMenu(BaseModel):
    menu_id = models.BigAutoField(primary_key=True, verbose_name='菜单ID')
    menu_name = models.CharField(max_length=50, verbose_name='菜单名称')
    parent_id = models.BigIntegerField(default=0, blank=True, null=True, verbose_name='父菜单ID')
    order_num = models.IntegerField(default=0, blank=True, verbose_name='显示顺序')
    path = models.CharField(max_length=200, default='', blank=True, verbose_name='路由地址')
    component = models.CharField(max_length=255, blank=True, null=True, verbose_name='组件路径')
    query = models.CharField(max_length=255, blank=True, null=True, verbose_name='路由参数')
    is_frame = models.IntegerField(default=1, verbose_name='是否为外链（0是 1否）')
    is_cache = models.IntegerField(default=0, verbose_name='是否缓存（0缓存 1不缓存）')
    menu_type = models.CharField(max_length=1, default='', verbose_name='菜单类型（M目录 C菜单 F按钮）')
    visible = models.CharField(max_length=1, default='0', verbose_name='菜单状态（0显示 1隐藏）')
    status = models.CharField(max_length=1, default='0', verbose_name='菜单状态（0正常 1停用）')
    perms = models.CharField(max_length=100, blank=True, null=True, verbose_name='权限标识')
    icon = models.CharField(max_length=100, default='#', blank=True, verbose_name='菜单图标')

    class Meta:
        db_table = 'yv_sys_menu'
        verbose_name = '菜单权限表'


class SysOperLog(BaseModel):
    """
    操作日志记录
    """
    oper_id = models.BigAutoField(primary_key=True, verbose_name="日志主键")
    title = models.CharField(max_length=50, blank=True, verbose_name="模块标题")
    business_type = models.IntegerField(default=0, verbose_name="业务类型（0其它 1新增 2修改 3删除）")
    method = models.CharField(max_length=100, blank=True, verbose_name="方法名称")
    request_method = models.CharField(max_length=10, blank=True, verbose_name="请求方式")
    operator_type = models.IntegerField(default=0, verbose_name="操作类别（0其它 1后台用户 2手机端用户）")
    oper_name = models.CharField(max_length=50, blank=True, verbose_name="操作人员")
    dept_name = models.CharField(max_length=50, blank=True, verbose_name="部门名称")
    oper_url = models.CharField(max_length=255, blank=True, verbose_name="请求URL")
    oper_ip = models.CharField(max_length=128, blank=True, verbose_name="主机地址")
    oper_location = models.CharField(max_length=255, blank=True, verbose_name="操作地点")
    oper_param = models.CharField(max_length=2000, blank=True, verbose_name="请求参数")
    json_result = models.CharField(max_length=2000, blank=True, verbose_name="返回参数")
    status = models.IntegerField(default=0, verbose_name="操作状态（0正常 1异常）")
    error_msg = models.CharField(max_length=2000, blank=True, verbose_name="错误消息")
    oper_time = models.DateTimeField(null=True, blank=True, verbose_name="操作时间")
    cost_time = models.BigIntegerField(default=0, verbose_name="消耗时间")

    class Meta:
        db_table = "yv_sys_oper_log"
        verbose_name = "操作日志记录"
        verbose_name_plural = verbose_name


class SysPost(BaseModel):
    """
    岗位信息表
    """
    post_id = models.BigAutoField(primary_key=True, verbose_name="岗位ID")
    post_code = models.CharField(max_length=64, verbose_name="岗位编码")
    post_name = models.CharField(max_length=50, verbose_name="岗位名称")
    post_sort = models.IntegerField(verbose_name="显示顺序")
    status = models.CharField(max_length=1, verbose_name="状态（0正常 1停用）")

    class Meta:
        db_table = "yv_sys_post"
        verbose_name = "岗位信息表"
        verbose_name_plural = verbose_name


class SysRole(BaseModel):
    """
    角色信息表
    """
    role_id = models.BigAutoField(primary_key=True, verbose_name="角色ID")
    role_name = models.CharField(max_length=30, verbose_name="角色名称")
    role_key = models.CharField(max_length=100, verbose_name="角色权限字符串")
    role_sort = models.IntegerField(verbose_name="显示顺序")
    data_scope = models.CharField(max_length=1, default="1", verbose_name="数据范围")
    menu_check_strictly = models.BooleanField(default=True, verbose_name="菜单树选择项是否关联显示")
    dept_check_strictly = models.BooleanField(default=True, verbose_name="部门树选择项是否关联显示")
    status = models.CharField(max_length=1, verbose_name="角色状态（0正常 1停用）")
    del_flag = models.CharField(max_length=1, default="0", verbose_name="删除标志（0代表存在 2代表删除）")

    class Meta:
        db_table = "yv_sys_role"
        verbose_name = "角色信息表"
        verbose_name_plural = verbose_name


class SysRoleDept(models.Model):
    """
    角色和部门关联表
    """
    role = models.ForeignKey('SysRole', on_delete=models.CASCADE, verbose_name="角色ID")
    dept_id = models.BigIntegerField(verbose_name="部门ID")

    class Meta:
        db_table = "yv_sys_role_dept"
        verbose_name = "角色和部门关联表"
        verbose_name_plural = verbose_name
        unique_together = ("role", "dept_id")


class SysRoleMenu(models.Model):
    """
    角色和菜单关联表
    """
    role = models.ForeignKey('SysRole', on_delete=models.CASCADE, verbose_name="角色ID")
    menu = models.ForeignKey('SysMenu', on_delete=models.CASCADE, verbose_name="菜单ID")

    class Meta:
        db_table = "yv_sys_role_menu"
        verbose_name = "角色和菜单关联表"
        verbose_name_plural = verbose_name
        unique_together = ("role", "menu")


# class SysUser(BaseModel):
#     """
#     用户信息表
#     """
#     user_id = models.BigAutoField(primary_key=True, verbose_name="用户ID")
#     dept_id = models.BigIntegerField(null=True, blank=True, verbose_name="部门ID")
#     user_name = models.CharField(max_length=30, verbose_name="用户账号")
#     nick_name = models.CharField(max_length=30, verbose_name="用户昵称")
#     user_type = models.CharField(max_length=2, default="00", verbose_name="用户类型（00系统用户）")
#     email = models.CharField(max_length=50, blank=True, verbose_name="用户邮箱")
#     phonenumber = models.CharField(max_length=11, blank=True, verbose_name="手机号码")
#     sex = models.CharField(max_length=1, default="0", verbose_name="用户性别（0男 1女 2未知）")
#     avatar = models.CharField(max_length=100, blank=True, verbose_name="头像地址")
#     password = models.CharField(max_length=100, blank=True, verbose_name="密码")
#     status = models.CharField(max_length=1, default="0", verbose_name="帐号状态（0正常 1停用）")
#     del_flag = models.CharField(max_length=1, default="0", verbose_name="删除标志（0代表存在 2代表删除）")
#     login_ip = models.CharField(max_length=128, blank=True, verbose_name="最后登录IP")
#     login_date = models.DateTimeField(null=True, blank=True, verbose_name="最后登录时间")
#
#     class Meta:
#         db_table = "yv_sys_user"
#         verbose_name = "用户信息表"
#         verbose_name_plural = verbose_name


class SysUserPost(models.Model):
    """
    用户与岗位关联表
    """
    # user = models.ForeignKey('User', on_delete=models.CASCADE, verbose_name="用户ID")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户ID")

    post = models.ForeignKey('SysPost', on_delete=models.CASCADE, verbose_name="岗位ID")

    class Meta:
        db_table = "yv_sys_user_post"
        verbose_name = "用户与岗位关联表"
        verbose_name_plural = verbose_name

        unique_together = ("user", "post")
